<?php
namespace app\api\controller;

use app\common\controller\ApiBase;
use app\common\model\Company;
use app\common\model\KfUser as KfUserModel;
use app\common\model\KfSessionMessage;
use app\common\model\User;

class KfUser extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
    }


    public function index()
    {
        if (!isset($this->param['cpm']) ||  empty($this->param['cpm'])){
            $this->ajaxReturn(false, 'cpm error');
        }
        $group=\Wechat::$customer_group;
        $group=array_combine(array_column($group,'str'),$group);
        $this->setCpmConfig($this->param['cpm']);
        $kf_user=KfUserModel::where('cpm',$this->param['cpm'])->where('status',1)->select()->each(function ($val) use($group){
            $user_info=\Wechat::getUserInfo($val['openid']);
            $val['wx_uname']=empty($user_info)? '':$user_info['nickname'];
            $val['is_online']=$val['expire_time']>time() ? 1:0;
            $group_name=[];
            if(!empty($val['group'])){
                $str=explode(',', $val['group']);
                foreach ($str as $key => $value) {
                    if(array_key_exists($value,$group)) $group_name[]=$group[$value]['content'];
                }
            }
            $val['group_name']=implode(',',$group_name);
        });
        $result=[];
        if(!$kf_user->isEmpty()){
            $result=$kf_user->toArray();
        }
        $this->ajaxReturn($result);
    }
    public function info()
    {
        if (!isset($this->param['id']) ||  empty($this->param['id'])){
            $this->ajaxReturn(false, 'id error');
        }
        $kf_user=KfUserModel::where('id',$this->param['id'])->find();
        if(empty($kf_user)){
            $this->ajaxReturn(false,'不存在该客服');
        }
        $this->setCpmConfig($kf_user['cpm']);
        $user_info=\Wechat::getUserInfo($kf_user['openid']);
        $kf_user['wx_uname']=empty($user_info)? '':$user_info['nickname'];
        $this->ajaxReturn($kf_user);
    }
    public function group()
    {
        $group=\Wechat::$customer_group;
        $this->ajaxReturn($group);
    }
    public function user()
    {
        if (!isset($this->param['cpm']) ||  empty($this->param['cpm'])){
            $this->ajaxReturn(false, 'cpm error');
        }
        $this->setCpmConfig($this->param['cpm']);
        $user=KfSessionMessage::where('cpm',$this->param['cpm'])->field('from')->group('from')->select()->each(function ($val){
            $user_info=\Wechat::getUserInfo($val['from']);
            $val['uname']=empty($user_info)? '':$user_info['nickname'];
        });
        $result=[];
        if(!$user->isEmpty()){
            $result=$user->toArray();
        }
        $this->ajaxReturn($result);
    }
    public function add()
    {
        if (!isset($this->param['cpm']) ||  empty($this->param['cpm'])){
            $this->ajaxReturn(false, 'cpm error');
        }
        if (!isset($this->param['uname']) ||  empty($this->param['uname'])){
            $this->ajaxReturn(false, '请填写名称');
        }
        if (!isset($this->param['group']) ||  empty($this->param['group'])){
            $this->ajaxReturn(false, '请选择分组');
        }
        if(!isset($this->param['openid']) ||  empty($this->param['openid'])){
            $this->ajaxReturn(false, '请选择绑定的用户');
        }
        $this->setCpmConfig($this->param['cpm']);
        $user_info=\Wechat::getUserInfo($this->param['openid']);
        if(empty($user_info) || !array_key_exists('headimgurl',$user_info)){
            $this->ajaxReturn(false,'用户不存在');
        }
        $kf_user= KfUserModel::where('cpm',$this->param['cpm'])->where('openid',$this->param['openid'])->find();
        if(!empty($kf_user)){
            $this->ajaxReturn(false,'已经存在');
        }
        $group=implode(',',$this->param['group']);
        // $data=[];
        // $data['kf_account']='kf'.rand(10000,99999).'@gh_be3c08dd5224';
        // $data['nickname']=$this->post['uname'];
        // $this->setCpmConfig($this->post['cpm']);
        // if(!\Wechat::addCustomer($data)){
        //     $this->ajaxReturn(false,'请重试');
        // }
        KfUserModel::create(['cpm'=>$this->param['cpm'],'openid'=>$this->param['openid'],'headimgurl'=>$user_info['headimgurl'],'uname'=>$this->param['uname'],'group'=>$group]);
        $this->ajaxReturn('ok','添加成功');
    }
    public function edit()
    {
        if (!isset($this->param['id']) ||  empty($this->param['id'])){
            $this->ajaxReturn(false, 'id error');
        }
        if (!isset($this->param['uname']) ||  empty($this->param['uname'])){
            $this->ajaxReturn(false, 'id error');
        }
        if (!isset($this->param['group']) ||  empty($this->param['group'])){
            $this->ajaxReturn(false, 'id error');
        }
        $kf_user=KfUserModel::where('id',$this->param['id'])->find();
        if(empty($kf_user)) $this->ajaxReturn(false,'客服不存在');
        $group=implode(',',$this->param['group']);
        KfUserModel::update(['group'=>$group,'uname'=>$this->param['uname']], ['id' => $this->param['id']]);
        $this->ajaxReturn('ok','修改成功');
    }
    public function delete()
    {
        if (!isset($this->param['id']) ||  empty($this->param['id'])){
            $this->ajaxReturn(false, 'id error');
        }
        $kf_user=KfUserModel::where('id',$this->param['id'])->where('status',1)->find();
        if(!empty($kf_user)){
            KfUserModel::where('id',$this->param['id'])->update(['status'=>0,'time'=>time()]);
        }
        $this->ajaxReturn('ok','删除成功');
    }
    /**
     * 公众号配置
     */
    private function setCpmConfig($cpm)
    {
        $company = Company::where('id', $cpm)->find();
        $token_arr = ['1' => 'ozvGRrYJ7555LJczRdckc7k75lZ57k2r', '2' => 'oO6bEV2oE40Z967226oeZ6EV065045iY', '3' => '', '4' => 'u9slLAJzoS00SJZjJP6SXS8LsjlxghsS', '5' => '', '6' => 'gterac3uzbb3u5fni30jc1owyg55euuj', '7' => 'xdnice2003abc', '8'=>'xdnice2019abc'];
        $appid = $company['wx_appid'];
        $appsecret = $company['wx_secret'];
        $token = $token_arr[$cpm];
        \Wechat::setConfig($appid, $appsecret, $token, $cpm);
        return true;
    }

}
